//
//  VistorView.swift
//  SinaBlog
//
//  Created by ChenWei on 15/10/3.
//  Copyright © 2015年 cw. All rights reserved.
//

import UIKit
import SnapKit

// MARK: - 自定义协议
@objc
protocol VistorViewDelegate: NSObjectProtocol {
    /// 点击了注册按钮
    optional func vistorViewDidRegigterBtnClick(vistorView: VistorView)
    
    /// 点击了登陆按钮
    optional func vistorViewDidLoginBtnClick(vistorView: VistorView)
}

class VistorView: UIView {
   
    /// 代理对象
    weak var deletage : VistorViewDelegate?
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        // 背景颜色
        backgroundColor = UIColor(red: 233 / 255.0, green: 233 / 255.0, blue: 233 / 255.0, alpha: 1)
        
        // 初始化子控件
        setupUI();
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
//        super.init(coder: aDecoder)  // 实现了这句,上面这句fetal就不用了,但是xib要设置好对应界面,不能和代码设置的冲突
    }
    
    // MARK: - 根据不同的控制器设置访客视图
    func setupVistorInfo(isHome isHome: Bool, imageName: String, info: String) {
       
        // 1.根据不同的控制器设置不同的内容
        icon.hidden = !isHome
        homeIcon.image = UIImage(named: imageName)
        messageLabel.text = info
        
        // 2. 判断是否是首页,如果是就执行动画
        if isHome {
           startAnimator()
        }
    }
    
    // MARK: - 内部方法
    /// 执行转盘动画
    func startAnimator() {
        // 1.创建基本核心动画
        let anim = CABasicAnimation()
        
        // 2.设置动画属性
        anim.keyPath = "transform.rotation"
//        anim.fromValue = 0
        anim.toValue = 2 * M_PI
        anim.duration = 20
        anim.repeatCount = MAXFLOAT
        anim.removedOnCompletion = false
        
        // 3.添加动画到图层上
        icon.layer.addAnimation(anim, forKey: nil)
        
    }
    
    
    /// 初始化化子控件
    func setupUI() {
        
        addSubview(icon)
        addSubview(maskBgView)
        addSubview(homeIcon)
        addSubview(messageLabel)
        addSubview(registerButton)
        addSubview(loginButton)
    }
    
    /// 布局子控件
    override func layoutSubviews() {
        super.layoutSubviews()
        
        // 1. 布局转盘
        icon.snp_makeConstraints { (make) -> Void in
            make.center.equalTo(self);
        }
        
        // 2. 布局图标
        homeIcon.snp_makeConstraints { (make) -> Void in
            make.center.equalTo(self)
        }
        
        // 3. 布局文本
        messageLabel.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(icon.snp_bottom)
            make.width.equalTo(224)
            make.centerX.equalTo(self)
        }
        
        // 4.布局注册按钮
        registerButton.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(messageLabel.snp_bottom).offset(20)
            make.width.equalTo(80)
            make.height.equalTo(44)
            make.right.equalTo(center).offset(-80)
        }
        
        // 5. 布局登陆按钮
        loginButton.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(registerButton)
            make.width.equalTo(registerButton)
            make.height.equalTo(registerButton)
            make.left.equalTo(center).offset(80)
        }
        
        // 6.背景图片
        maskBgView.snp_makeConstraints { (make) -> Void in
            make.edges.equalTo(0)
        }
        
    }

    /// 监听注册按钮
    func registerBtnClick() {
        deletage?.vistorViewDidRegigterBtnClick!(self)
//        print(__FUNCTION__)
    }
    
     /// 监听登陆按钮
    func loginBtnClick() {
        deletage?.vistorViewDidLoginBtnClick!(self)
//        print(__FUNCTION__)
    }

    // MARK: - 懒加载
    /// 转盘
    private lazy var icon : UIImageView = {
        let icon = UIImageView(image: UIImage(named: "visitordiscover_feed_image_smallicon"))
        return icon
    }()
    
    /// 图标
    private lazy var homeIcon : UIImageView = {
        let homeIcon = UIImageView(image: UIImage(named: "visitordiscover_feed_image_house"))
        return homeIcon
    }()
    
    /// 文本
    private lazy var messageLabel : UILabel = {
        let messageLabel = UILabel()
        messageLabel.text = "j时村禁桂ssflsf桂林村村村国不这鞍村官村村村霜要霜"
        messageLabel.textColor = UIColor.grayColor()
        messageLabel.textAlignment = NSTextAlignment.Center
        
        messageLabel.frame.size.width = 220
        messageLabel.numberOfLines = 0
        messageLabel.sizeToFit()
        return messageLabel
        
    }()
    
    /// 注册按钮
     private lazy var registerButton : UIButton = {
        let btn = UIButton()
        
        btn.setTitle("注册", forState: UIControlState.Normal)
        btn.setTitleColor(UIColor.grayColor(), forState: UIControlState.Normal)
        btn.setTitleColor(UIColor.orangeColor(), forState: UIControlState.Highlighted)
        
        btn.setBackgroundImage(UIImage(named: "common_button_white_disable"), forState: UIControlState.Normal)
        
        btn.addTarget(self, action: Selector("registerBtnClick"), forControlEvents: UIControlEvents.TouchUpInside)
        
        return btn
    }()
    
    /// 登陆按钮
    private lazy var loginButton : UIButton = {
        let btn = UIButton()
        
        btn.setTitle("登陆", forState: UIControlState.Normal)
        btn.setTitleColor(UIColor.grayColor(), forState: UIControlState.Normal)
        btn.setTitleColor(UIColor.orangeColor(), forState: UIControlState.Highlighted)
        
        btn.setBackgroundImage(UIImage(named: "common_button_white_disable"), forState: UIControlState.Normal)
        
        btn.addTarget(self, action: Selector("loginBtnClick"), forControlEvents: UIControlEvents.TouchUpInside)
        return btn
    }()
    
    /// 背影图片
    private lazy var maskBgView : UIImageView = {
        let maskBgView = UIImageView()
        maskBgView.image = UIImage(named: "visitordiscover_feed_mask_smallicon")
        
        return maskBgView
    }()
}
